/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VariableView2.java
 *
 * Created on Feb 27, 2009, 4:42:08 AM
 */

package PET;

import PET.model.PersistentSettings;
import PET.model.SettingsMgr;
import PET.model.Variables;
import java.awt.Font;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author crayment
 */
public class VariableView extends javax.swing.JPanel implements Observer{

    private DefaultTableModel table;
    private Vector data = new Vector();

    /** Creates new form VariableView2 */
    public VariableView() {
        initComponents();

        Vector colNames = new Vector();
        colNames.add("Name"); colNames.add("Value");

        table = new DefaultTableModel(data, colNames);

        VariablesTable.setModel(table);

        setCustomSettings();
    }

    public void update(Observable o, Object arg) {
        if(arg instanceof String)
        {
            try{
                String name = (String)arg;
                if(!Variables.isDefined(name))
                    deleteVariable(name);
                else
                {
                    Object value = Variables.get(name);
                    if(isVariable(value)) {
                        if(value instanceof String) {
                            value = "\""+value+"\"";
                        }
                        updateVariable(name, value);
                    }
                }
            }
            catch(Exception e){System.err.println(e);/*implement me!!*/}
        }
    }

    private void addVariable(String name, Object value)
    {
        Vector row = new Vector();
        row.add(name); row.add(value);

        data.add(row);
        updateView();
    }

    private void updateVariable(String name, Object value)
    {
        Vector row = getRow(name);
        if(row != null)
        {
            row.removeElementAt(1);
            row.addElement(value);
            updateView();
        }
        else
        {
            addVariable(name, value);
        }
    }

    private void deleteVariable(String name)
    {
        int index = getIndexOf(name);
        if(index >= 0)
            data.removeElementAt(index);

        updateView();
    }

    private Vector getRow(String name)
    {
        for(int i=0; i<data.size(); i++)
        {
            Vector row = (Vector)data.elementAt(i);
            if(row.elementAt(0).equals(name))
                return row;
        }
        return null;
    }

    private int getIndexOf(String name)
    {
        for(int i=0; i<data.size(); i++)
        {
            Vector row = (Vector)data.elementAt(i);
            if(row.elementAt(0).equals(name))
                return i;
        }
        return -1;
    }

    private Boolean isVariable(Object o)
    {
        if(o instanceof Integer) return true;
        if(o instanceof Boolean) return true;
        if(o instanceof String) return true;
        return false;
    }
    private void updateView()
    {
        table.fireTableDataChanged();
    }

    
    public void setCustomSettings()
    {
        try {
            PersistentSettings ps = new PersistentSettings();
            SettingsMgr sm = new SettingsMgr();
            ps = (PersistentSettings) sm.readObject(ps.getClass().getName());
            if (ps == null) //Write the default object
            {
                ps = new PersistentSettings();
                sm.writeObject(ps);
            }
            Font font = new Font(ps.getFontType(), Font.PLAIN, ps.getFontSize());
            this.VariablesTable.setFont(font);
        }

        catch (IOException ex)
        {
            Logger.getLogger(Terminal.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(LineNumberedTextArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        VariablesScrollPane = new javax.swing.JScrollPane();
        VariablesTable = new javax.swing.JTable();
        VarLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        VariablesScrollPane.setName("VariablesScrollPane"); // NOI18N

        VariablesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        VariablesTable.setName("VariablesTable"); // NOI18N
        VariablesScrollPane.setViewportView(VariablesTable);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(PET.PETApp.class).getContext().getResourceMap(VariableView.class);
        VarLabel.setText(resourceMap.getString("VarLabel.text")); // NOI18N
        VarLabel.setName("VarLabel"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(VariablesScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .add(VarLabel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(VarLabel)
                .add(4, 4, 4)
                .add(VariablesScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel VarLabel;
    private javax.swing.JScrollPane VariablesScrollPane;
    private javax.swing.JTable VariablesTable;
    // End of variables declaration//GEN-END:variables



}
